<template>
   <div class="login_container">
       <div class="trom_container">
           <img src="@/assets/logo.png" alt="">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm"  class="demo-ruleForm">
                <el-form-item  prop="username">
                    <el-input v-model="ruleForm.username" prefix-icon="el-icon-s-custom"></el-input>
                </el-form-item>
                <el-form-item  prop="password">
                    <el-input v-model="ruleForm.password" type="password" prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <el-form-item class="login_btn">
                    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                    <el-button @click="resetForm('ruleForm')" type="info">重置</el-button>
                </el-form-item>
            </el-form>
       </div>
   </div>
</template>
<script>
import { loginApi } from '@/api'
export default {
  data () {
    return {
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]

      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            const res = await loginApi(this.ruleForm)
            localStorage.setItem('token', res.data.data.token)
            console.log(res.data.meta.status)
            if (res.data.meta.status === 200) {
              this.$router.push('/')
              this.$message({
                message: '成功了窝',
                type: 'success'
              })
            } else {
              this.$message.error('错了密码错了窝！！重新输一下')
            }
          } catch (error) {
            console.log(error)
          }
        } else {
          console.log('输入正确的用户名和密码')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang='less' scoped>
.login_container{
    position: relative;
    width: 100%;
    height: 100%;
    background-color: #2c4967;
    .trom_container{
        width: 450px;
        height: 304px;
        border-radius: 3px;
        background-color: #fff;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        box-shadow: 0 0 10px 3px #ccc;
        box-sizing: border-box;
        .demo-ruleForm{
            padding: 82px 28px 0;
            .login_btn{
                text-align: right;
            }
        }
        img{
            position: absolute;
            left: 50%;
            transform: translate(-50%,-50%);
            width: 130px;
            height: 130px;
            border-radius: 50%;
            border:8px solid #fff;
            box-shadow: 0 0 10px 3px #ccc;
            background-color: #efefef;
        }
    }
}
</style>
